package j20251127;

import java.util.PriorityQueue;

class KthLargest {
    PriorityQueue<Integer> heap;
    int k;
    public KthLargest(int _k, int[] nums) {
        k=_k;
        heap=new PriorityQueue<>();
        for(int x:nums){
            heap.offer(x);
            if(heap.size()>k){
                heap.poll();
            }
        }
    }

    public int add(int val) {
        heap.offer(val);
        if(heap.size()>k){
            heap.poll();
        }
        return heap.peek();
    }
}
public class Solution2 {
    public static void main(String[] args) {
        int[] nums={4,5,8,2};
        KthLargest kthLargest = new KthLargest(3, nums);
        System.out.println(kthLargest.add(3));   // return 4
        System.out.println(kthLargest.add(5));   // return 5
        System.out.println(kthLargest.add(10));  // return 5
        System.out.println(kthLargest.add(9));   // return 8
        System.out.println(kthLargest.add(4));   // return 8
    }
}
